package com.bazaaroid.mobile.android;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bazaaroid.mobile.db.model.partner.Partner;
import com.bazaaroid.mobile.db.model.partner.Product;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapProductOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private String TAG = "[MapProductOverlay]";
	private int displayWidth = -1;
	private Product product = null;
	
	public MapProductOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public MapProductOverlay(Drawable defaultMarker, Context context){
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	public void setDisplayWidth(int displayWidth)
	{
		this.displayWidth = displayWidth;
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	/*
	 * Needed in order to add new OverlayItems to the ArrayList
	 */
	public void addOverlay(OverlayItem overlay){
		mOverlays.add(overlay);
		// call the populate() for the ItemizedOverlay that will read each of the OverlayItems and prepare them to be drown.
		populate();
	}
	
	/*
	 * Overriding the onTap(int) callback method, which will handle the event when an item is tapped by the user.
	 */
	@Override
	protected boolean onTap(int index)
	{
		//OverlayItem item = mOverlays.get(index);
		/*AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();*/
		
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.product_loc_details);
		dialog.setCancelable(true);	    	
		
		// read the display settings
		/*
		Display display = getWindowManager().getDefaultDisplay(); 
		int displayWidth = display.getWidth();
		*/
		
		displayMapItemDetailsDialog(product);
		
		/*
		// longitude, latitude, state, city, street, ok
		TextView centerLongitude = null;
		TextView centerLatitude = null;
		TextView centerState = null;
		TextView centerCity = null;
		TextView centerStreet = null;
		Button centerOkButton = null;
		GeoPoint centerPoint = getCenter();
		Geocoder gcd = null;
		List<Address> addresses = null;
		
		Address currentAddress = null;
		String country = null;
		String city = null;
		String countryCode = null;
		String addressLine = null;
		String latitude = null;
		String longitude = null;
		double longitudeValue = 0.0;
		double latitudeValue = 0.0; 
		
		LinearLayout centerLocDetails = null;
		
		centerLocDetails = (LinearLayout)dialog.findViewById(R.id.center_loc_details);
		centerLocDetails.setMinimumWidth((int)(displayWidth * 0.8));
		
		gcd = new Geocoder(mContext);//, Locale.getDefault()); 
		try
		{
			latitudeValue = (centerPoint.getLatitudeE6() / 1E6);
			longitudeValue = (centerPoint.getLongitudeE6() / 1E6);
			addresses = gcd.getFromLocation(latitudeValue, longitudeValue, 1); 
		}
		catch(IOException ioe)
		{
			Log.e(TAG, ioe.getMessage());
		}
		
		if (addresses != null && addresses.size() > 0)  
		{
			currentAddress = addresses.get(0);
			country = currentAddress.getCountryName();
			countryCode = currentAddress.getCountryCode();
			city = currentAddress.getLocality();
			addressLine = currentAddress.getAddressLine(0);
			longitude = String.valueOf(currentAddress.getLongitude());
			latitude = String.valueOf(currentAddress.getLatitude());
		}

		
		centerLongitude = (TextView)dialog.findViewById(R.id.center_loc_longitude);
		centerLongitude.setText(String.format("%s %s", mContext.getString(R.string.center_loc_longitude_text), longitude));
		
		centerLatitude = (TextView)dialog.findViewById(R.id.center_loc_latitude);
		centerLatitude.setText(String.format("%s %s", mContext.getString(R.string.center_loc_latitude_text), latitude));
		
		if(country != null && country != "")
		{
			centerState = (TextView)dialog.findViewById(R.id.center_loc_state);
			centerState.setText(String.format("%s %s [%s]", mContext.getString(R.string.center_loc_country_text), country, countryCode));
		}
		
		if(city != null && city != "")
		{
			centerCity = (TextView)dialog.findViewById(R.id.center_loc_city);
			centerCity.setText(String.format("%s %s", mContext.getString(R.string.center_loc_city_text), city));
		}
		
		if(addressLine != null && addressLine != "")
		{
			centerStreet = (TextView)dialog.findViewById(R.id.center_loc_street);
			centerStreet.setText(String.format("%s %s", mContext.getString(R.string.center_loc_street_text), addressLine));
		}
		
		centerOkButton = (Button)dialog.findViewById(R.id.center_loc_ok_button);
		centerOkButton.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View v){
    			//finish();
    			dialog.dismiss();
    		}
    	});
    	
    	dialog.show();		*/
		return true;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		super.draw(canvas, mapView, false/*shadow*/);
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}
	
    void displayMapItemDetailsDialog(Product product)
    {
    	final Dialog dialog = new Dialog(mContext);
    	dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	dialog.setContentView(R.layout.product_loc_details);
    	//dialog.setTitle(getString(R.string.selected_item_title));
    	dialog.setCancelable(true);
    	
    	ImageView productImage = null;
    	TextView productName = null;
    	TextView productDescription = null;
    	TextView productPrice = null;
    	TextView productCurrency = null;
    	
    	ImageView partnerImage = null;
    	TextView partnerName = null;
    	TextView partnerDescription = null;
    	TextView partnerAddress = null;
    	TextView partnerTelephone = null;
    	TextView partnerMobile = null;
    	TextView partnerEmail = null;
    	TextView partnerSite = null;
    	
    	Button okButton = null;
    	LinearLayout listItemDetails = null;
    	LinearLayout productHolder = null;
    	LinearLayout partnerHolder = null;
    	LinearLayout productDetails = null;
    	LinearLayout partnerDetails = null;
    	LinearLayout priceAndCurrency = null;
    	
    	

    	
    	listItemDetails = (LinearLayout)dialog.findViewById(R.id.map_item_details);
    	listItemDetails.setMinimumWidth((int)(displayWidth));
    	//listItemDetails.setMinimumHeight((int)(displayWidth * 0.8));
    	
    	productHolder = (LinearLayout)dialog.findViewById(R.id.selected_item_product);
    	partnerHolder = (LinearLayout)dialog.findViewById(R.id.selected_item_partner);
    	okButton = (Button)dialog.findViewById(R.id.map_item_ok_button);
    	
    	productDetails = (LinearLayout)productHolder.getChildAt(1);
    	partnerDetails = (LinearLayout)partnerHolder.getChildAt(1);
    	
    	
    	
    	productImage = (ImageView)productHolder.getChildAt(0);//(ImageView)dialog.findViewById(R.id.selected_item_image);
    	
    	productName = (TextView)productDetails.getChildAt(0);//(TextView)dialog.findViewById(R.id.selected_item_name);
    	productName.setText(product.getName());
    	
    	productDescription = (TextView)productDetails.getChildAt(1);//(TextView)dialog.findViewById(R.id.selected_item_name);
    	productDescription.setText(product.getDescription());
    	
    	priceAndCurrency = (LinearLayout)productDetails.getChildAt(2);
    	
    	productPrice = (TextView)priceAndCurrency.getChildAt(0);//(TextView)dialog.findViewById(R.id.selected_item_name);
    	productPrice.setText(String.valueOf((int)product.getPrice()));
    	
    	productCurrency = (TextView)priceAndCurrency.getChildAt(1);//(TextView)dialog.findViewById(R.id.selected_item_name);
    	productCurrency.setText(product.getCurrency());
    	
    	
    	
/*    	ImageView partnerImage = null;
    	TextView partnerName = null;
    	TextView partnerDescription = null;
    	TextView partnerAddress = null;
    	TextView partnerTelephone = null;
    	TextView partnerMobile = null;
    	TextView partnerEmail = null;
    	TextView partnerSite = null;*/
    	partnerImage = (ImageView)partnerHolder.getChildAt(0);
    	
    	Partner partner = product.getPartner();
    	
    	partnerName = (TextView)partnerDetails.getChildAt(0);
    	partnerName.setText(partner.getName());
    	
    	partnerDescription = (TextView)partnerDetails.getChildAt(1);
    	partnerDescription.setText(partner.getDescription());
    	
    	partnerAddress = (TextView)partnerDetails.getChildAt(2);
    	// TODO: Read GPS values!!!!!
    	partnerAddress.setText("");
    	
    	partnerTelephone = (TextView)partnerDetails.getChildAt(3);
    	partnerTelephone.setText(mContext.getString(R.string.partner_phone) + " " + partner.getContact().getTelephone());
    	
    	partnerMobile = (TextView)partnerDetails.getChildAt(4);
    	partnerMobile.setText(mContext.getString(R.string.partner_cell) + " " + partner.getContact().getMobile());
    	
    	partnerEmail = (TextView)partnerDetails.getChildAt(5);
    	partnerEmail.setText(partner.getContact().getEmail());
    	
    	partnerSite = (TextView)partnerDetails.getChildAt(6);
    	partnerSite.setText(partner.getContact().getSite());
    	
    	//backButton = (Button)buttonHolder.getChildAt(0);//(Button)dialog.findViewById(R.id.selected_item_back_button);
    	okButton.setWidth(displayWidth);
    	okButton.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View v){
    			//finish();
    			dialog.dismiss();
    		}
    	});
    	
    	dialog.show();    	
    }	
}
